

ALTER    PROCEDURE sp_update_rma_line_items

 @LINEITEM_ID    INT,
 @RMA_ID         INT,
 @PART_NUMBER    VARCHAR(50),
 @INVOICE_NUMBER VARCHAR(50), 
 @INVOICE_DATE   DATETIME,
 @SERIAL_NUMBER  VARCHAR(50),
 @PROBLEM_DESC   VARCHAR(500),
 @REASON         VARCHAR(500),
 @REMARKS        VARCHAR(500),
 @QUANTITY       INT,
 @PO_LINEITEM_NO INT,
 @PO_NUMBER      VARCHAR(50),
 @UNIT_PRICE	 FLOAT,
 @EXCHANGE       INT,
 @CREDIT         INT,
 @FA             INT,
 @CLOSE_DATE     DATETIME,
 @CLOSE_QTY      INT,
 @RECEIVE_QTY      INT,
 @SALES_ORDER_NO  VARCHAR(50)
AS
BEGIN
	declare @status varchar(10)
	-- declare @totalLineItems int
	-- declare @closeLineItems int
	-- declare @rmaCloseDate datetime
	
	declare @FINAL_QTY int
	declare @SPLIT_QTY int
	declare @LineItem_Number int

	set @FINAL_QTY = @QUANTITY 

	if (  ( @CLOSE_DATE is not null ) and  ( @QUANTITY = @CLOSE_QTY ) ) 
	begin
	     set @status = 'CLOSE'
	end
	else if ( ( @CLOSE_DATE is not null ) and ( @QUANTITY <> @CLOSE_QTY ) and ( @CLOSE_QTY > 0 ) )
	begin
	     set @status = 'CLOSE'	
	     set @FINAL_QTY = @CLOSE_QTY	
	     set @SPLIT_QTY = @QUANTITY - @CLOSE_QTY	
	end
	
	UPDATE RMA_Line_Item_Table
	SET PO_LineItem_Number = @PO_LINEITEM_NO ,
	    PO_Number = @PO_NUMBER , 	
	    Part_Number    = @PART_NUMBER ,
	    Invoice_Number = @INVOICE_NUMBER ,
	    Invoice_Date   = @INVOICE_DATE ,
	    Serial_Number  = @SERIAL_NUMBER ,
	    Problem_Description = @PROBLEM_DESC ,
	    Reason = @REASON ,
	    Remarks = @REMARKS ,
	    Quantity = @FINAL_QTY ,
	    Unit_Price = @UNIT_PRICE,
	    IS_Exchange = @EXCHANGE,
	    IS_Credit =  @CREDIT,
	    IS_FA = @FA, 	
	    Status = @status,
	    Close_Date = @CLOSE_DATE,				
	    Receive_qty = @RECEIVE_QTY,
	    SALES_ORDER_NO = @SALES_ORDER_NO,
	    --IS_APPROVED = @IS_APPROVED,		
	    Updated_On = CURRENT_TIMESTAMP			    	 				
	WHERE RMA_ID = @RMA_ID AND RMA_LineItem_ID = @LINEITEM_ID

	if ( @SPLIT_QTY > 0 )
	begin
		select @LineItem_Number =   max(isNull(LineItem_Number,0)) + 1  from RMA_Line_Item_Table where RMA_ID = @RMA_ID
			
		insert into RMA_Line_Item_Table(RMA_ID, LineItem_Number, Part_Number, Invoice_Number, Invoice_Date, Quantity, Serial_Number, Reason, Problem_Description, Remarks, Created_On, Updated_On,PO_LineItem_Number,PO_Number,Unit_Price,IS_Exchange,IS_Credit,IS_FA , Status , Close_Date, CLOSE_QTY, RECEIVE_QTY, SALES_ORDER_NO, IS_APPROVED) 
		values(@RMA_ID, @LineItem_Number, @PART_NUMBER, @INVOICE_NUMBER, @INVOICE_DATE, @SPLIT_QTY, @SERIAL_NUMBER, @REASON, @PROBLEM_DESC, NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, @PO_LINEITEM_NO, @PO_NUMBER, @UNIT_PRICE, @EXCHANGE, @CREDIT, @FA , NULL , NULL, @CLOSE_QTY, @RECEIVE_QTY, @SALES_ORDER_NO, 1)	
	end
	/* Commented By Basha (11/19/2004)
	select @totalLineItems = count(*) from RMA_Line_Item_Table where RMA_ID = @RMA_ID 
	select @closeLineItems = count(*) from RMA_Line_Item_Table where RMA_ID = @RMA_ID AND Status = 'CLOSE'
	if ( @totalLineItems <> 0 and @closeLineItems = @totalLineItems )
	begin
		select @rmaCloseDate = max(Close_Date) from RMA_Line_Item_Table where RMA_ID = @RMA_ID
		update RMA_Table set Close_Date = @rmaCloseDate , RMA_Status = 'CLOSE' where RMA_ID = @RMA_ID	 
	end
	*/
END




